package com.dingshi.security.api;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.dingshi.security.config.HttpClientConfig;
import com.dingshi.security.entity.Detail;
import com.dingshi.security.net.HttpResult;
import com.dingshi.security.net.HttpResultJson;
import com.dingshi.security.net.NetService;

public class RepairApi extends BaseApi {

	private static final String TAG = "RepairApi";

	public RepairApi(Context context) {
		super(context);
	}

	public void UploadImage(final int requestCode,final String filepath) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				try {
					Log.e(TAG, "filepath is " + filepath);
					FileInputStream fis = new FileInputStream(filepath);
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int count = 0;
					while ((count = fis.read(buffer)) >= 0) {
						baos.write(buffer, 0, count);
					}
//					String uploadBuffer = new String(Base64.encode(baos.toByteArray())); // 进行Base64编码
//					Log.e(TAG, "uploadBuffer is " + uploadBuffer.length());
					// //调用webservice  
					HttpResultJson result = NetService.uploadImage(context, filepath, baos.toByteArray(), HttpClientConfig.UPLOAD_URL);
					
					if (result.getResultCode() == HttpResult.RESULT_OK) {
						apiResult.setResultCode(ApiResult.RESULT_OK);

					} else {
						apiResult.setFailCode(result.getFailCode());
						apiResult.setFailMessage(result.getFailMessage());
					}
					fis.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return apiResult;
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}

	/**
	 * 
	 * @param requestCode
	 * @param aid
	 * @param did
	 * @param devicesId
	 * @param in
	 * @param description
	 * @param list
	 */
	public void createRepair(final int requestCode,final String aid,final String did,
			final String devicesId,final String in,final String description,final
			ArrayList<String> list) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ArrayList<String> tempList = new ArrayList<String>();
				if(list!=null) {
					for(int i=0;i<list.size();i++) {
						tempList.add(uploadFile(list.get(i)));
					}
				}

				//上传其他信息
				JSONObject obj = new JSONObject();
				try {
					obj.put("s", aid);
					obj.put("d", did);
					obj.put("e", devicesId);
					obj.put("in", in);
					obj.put("de", description);
					if(tempList!=null) {
						for(int i=0;i<tempList.size();i++) {
							obj.put("p"+(i+1), tempList.get(i));
						}
					}
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				Log.e(TAG, "request params "+obj.toString());
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.ADD_REPAIR_URL);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);

				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}
	
	private String uploadFile(String filepath) {
		Log.e("Filepath", "filepath is " + filepath);
		FileInputStream fis;
		try {
			fis = new FileInputStream(filepath);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int count = 0;
			while ((count = fis.read(buffer)) >= 0) {
				baos.write(buffer, 0, count);
			}
			fis.close();
			// String uploadBuffer = new
			// String(Base64.encode(baos.toByteArray())); // 进行Base64编码
			// Log.e(TAG, "uploadBuffer is " + uploadBuffer.length());
			// //调用webservice  
			HttpResultJson result = NetService.uploadImage(context, filepath,baos.toByteArray(), HttpClientConfig.UPLOAD_URL);
			if(result!=null) {
				return result.getResult();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 
	 * @param requestCode
	 * @param nid 人员编号
	 * @param loginuserType
	 */
	public void getRepairList(final int requestCode,final String nid,final String loginuserType) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("nid", nid);
					obj.put("t", loginuserType);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.GET_REPAIRBILL_LIST);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}
	/**
	 * 
	 * @param requestCode
	 * @param nid
	 * @param loginuserType
	 */
	public void getRepairDetail(final int requestCode,final String orderNum,final String contentType) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("i", orderNum);
					obj.put("t", contentType);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.GET_REPAIRBILL_DETAIL);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}

	public void cancelTrack(final int requestCode,final String name,final String oddNum) {
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("s", name);
					obj.put("r", oddNum);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.CANCEL_TRACK);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}

	public void confirmReceive(final int requestCode, final String guardNum, final String oddNum) {
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("w", guardNum);
					obj.put("r", oddNum);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.CONFIRM_RECEIVE);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}
	public void confirmRepair(final int requestCode, final String guardNum, final String oddNum) {
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("s", guardNum);
					obj.put("r", oddNum);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.CONFIRM_REPAIR);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}

	public void updateRepair(final int requestCode,final String oddNum, final Detail detail) {
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("r", oddNum);
					obj.put("edt", detail.getEdt());
					obj.put("rer", detail.getRer());
					obj.put("rea", detail.getRea());
					obj.put("res", detail.getRes());
					obj.put("st", detail.getSt());
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(obj.toString(),context,HttpClientConfig.UPDATE_REPAIR);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
				super.onPostExecute(apiResult);
			}
			
		}.execute();
	}
}
